import rclpy
from rclpy.node import Node
from std_msgs.msg import String
import time
import pyttsx3

def call(massage):
    link=massage.data
    print("xiaoxi:{}".format(link))
    lanage="Mandarin"
    lanage1="cantonese"
    # 设置发音速率，默认值为200
    rate = engine.getProperty('rate')
    engine.setProperty('rate', rate)
    # 设置发音大小，范围为0.0-1.0
    volume = engine.getProperty('volume')
    engine.setProperty('volume', 1)
    voices = engine.getProperty('voices')
    engine.setProperty('voice', lanage1)
    # 添加朗读文本
    engine.say(link)
    # 等待语音播报完毕
    engine.runAndWait()

def main (args=None):
    
    global engine
    engine = pyttsx3.init()

    rclpy.init(args=args)#初始化rclpy
    node=Node("speak")#新建一个节点
    node.get_logger().info("功能启动!!!")
    node.sub_novel=node.create_subscription(String,"QR_Code_Content",call,10)


    rclpy.spin(node)#保持节点运行 检测是否收到退出指令  （Ctrl+c）
    rclpy.shutdown()#关闭rclpy